import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class MsgHandle {

    public void sendToAll(ConcurrentHashMap<String, SocketChannel> map, SelectionKey sk,String msg){


        map.forEach((key, value) ->
                {
                    if (value == (SocketChannel)sk.channel()){
                        ByteBuffer msgBuffer = ByteBuffer.wrap((key+msg).getBytes());
                        int maxBytesPerRead  = 1024;
                        List<SocketChannel> channelList = new ArrayList<>(map.values());
                        byte[] bytes = new byte[maxBytesPerRead];
                        while (msgBuffer.hasRemaining()){
                            int bytesToWrite = Math.min(msgBuffer.remaining(), maxBytesPerRead);
                            msgBuffer.get(bytes, 0, bytesToWrite);
                            ByteBuffer writeBuffer = ByteBuffer.wrap(bytes, 0, bytesToWrite);
                            for (SocketChannel channel : channelList) {
                                if(channel != (SocketChannel)sk.channel()){ //保证群发不向自己发内容
                                    try {
                                        channel.write(writeBuffer);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    writeBuffer.rewind();
                                }
                            }
                        }

                    }

                });
    }

    public void sentToOne(ConcurrentHashMap<String, SocketChannel> map, SelectionKey sk,String msg){
        ByteBuffer msgBuffer = ByteBuffer.wrap(msg.getBytes());

        int maxBytesPerRead  = 1024;

        map.forEach((key, value) ->
        {

            if(key.equals(msg.substring(0,msg.indexOf("@!#")))){
                byte[] bytes = new byte[maxBytesPerRead];
                while (msgBuffer.hasRemaining()){
                    int bytesToWrite = Math.min(msgBuffer.remaining(), maxBytesPerRead);
                    msgBuffer.get(bytes, 0, bytesToWrite);
                    ByteBuffer writeBuffer = ByteBuffer.wrap(bytes,0,bytesToWrite);
                    try {
                        value.write(writeBuffer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void sentMemberInfo(SocketChannel sk,ConcurrentHashMap<String, SocketChannel> map){
        String msg = "%^&";//标识为传输聊天室成员


        int maxBytesPerRead  = 1024;

        // 获取key-value对的Set集合
        Set<Map.Entry<String, SocketChannel>> entrySet = map.entrySet();

        // 遍历Set集合中每个entry，并进行操作
        for (Map.Entry<String, SocketChannel> entry : entrySet) {
            String key = entry.getKey();
            msg += key+"#";//�õ����г�Ա


        }
        ByteBuffer msgBuffer = ByteBuffer.wrap(msg.getBytes());//初始化发送消息

        //发送成员给所有人
        List<SocketChannel> channelList = new ArrayList<>(map.values());
        byte[] bytes = new byte[maxBytesPerRead];
        while (msgBuffer.hasRemaining()){
            int bytesToWrite = Math.min(msgBuffer.remaining(), maxBytesPerRead);
            msgBuffer.get(bytes, 0, bytesToWrite);
            ByteBuffer writeBuffer = ByteBuffer.wrap(bytes, 0, bytesToWrite);
            for (SocketChannel channel : channelList) {
                    try {
                        channel.write(writeBuffer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    writeBuffer.rewind();

            }
        }


    }
}
